﻿using System;
using System.Collections;
using System.IO;
using SSTestAPI;
using System.Text;

namespace Data_Storages
{
    public class DSCsv : IDataStorage
    {
        private string Filepath { get; set; }

        public DSCsv(string filepath)
        {
            Filepath = filepath;
        }

        public ArrayList Load()
        {
            var plantsList = new ArrayList();
            try
            {
                using (var sr = new StreamReader(Filepath))
                {
                    string line;
                    sr.ReadLine();
                    while ((line = sr.ReadLine()) != null)
                    {
                        var row = line.Split(';');
                        switch (row[0])
                        {
                            case "Tree":
                                {
                                    plantsList.Add(new Tree(row[1], row[3], Boolean.Parse(row[4]), 
                                        Int32.Parse(row[2])));
                                    break;
                                }
                            case "Flower":
                                {
                                    plantsList.Add(new Flower(row[1], Boolean.Parse(row[5]), 
                                        Int32.Parse(row[6]), Int32.Parse(row[2])));
                                    break;
                                }
                            default:
                                break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return plantsList;
        }

        public void Save(ArrayList plantsList)
        {
            if (plantsList == null || plantsList.Count == 0)
            {
                throw new Exception("Error: Empty list");
            }
            try
            {
                using (var sr = new StreamWriter(Filepath, false, Encoding.Unicode))
                {
                    const string header = "PlantType;Branch;Lifetime;TreeGroup;TreeFruit;FlowerSymmetric;FlowerLeafs";
                    sr.WriteLine(header);
                    foreach (object ob in plantsList)
                    {
                        string row;
                        if (ob is Tree)
                        {
                            row = "Tree;" +
                                ((Tree)ob).Branch + ";" +
                                ((Tree)ob).getLifetime() + ";" +
                                ((Tree)ob).Group + ";" +
                                ((Tree)ob).Fruit + ";;";
                            sr.WriteLine(row);
                        }
                        else if (ob is Flower)
                        {
                            row = "Flower;" +
                                ((Flower)ob).Branch + ";" +
                                ((Flower)ob).getLifetime() + ";" +
                                ";;" +
                                ((Flower)ob).Symmetric + ";" +
                                ((Flower)ob).LeafsQuantity;
                            sr.WriteLine(row);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
